/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.edu.kamai.xmlparser.speaking;

import com.edu.kamai.xml.beans.speaking.speakingAnswer;
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 *
 * @author xyzhu
 */
public class speakingAnswerParser {
    public speakingAnswerParser(String pp){
        paperPath = pp;
        
    }
    
    public speakingAnswer parseWritingAnswerXML(){
        speakingAnswer sa = null;
        try{
            File xmlFile = new File(paperPath);
            if(xmlFile.exists()){
                DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
                DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
                Document doc = dBuilder.parse(xmlFile);
                doc.getDocumentElement().normalize();
                
                
                NodeList nList = doc.getElementsByTagName("speakingAnswer");
                sa = new speakingAnswer(nList.getLength());
                for (int temp = 0; temp < nList.getLength(); temp++) {
                    Node nNode = nList.item(temp);
                    if (nNode.getNodeType() == Node.ELEMENT_NODE) {
                        Element eElement = (Element) nNode;
                        System.out.println("speakingMp3Path: " + getTagValue("speakingMp3Path", eElement));
                        String mp3path = getTagValue("speakingMp3Path", eElement);
                        sa.setSpeakingMp3Path(mp3path);
                    }
                }
            }
        }catch(Exception ex){
            ex.printStackTrace();
        }
        return sa;
    }
    
    private static String getTagValue(String sTag, Element eElement) {
	NodeList nlList = eElement.getElementsByTagName(sTag).item(0).getChildNodes();
        Node nValue = (Node) nlList.item(0);
        if(nValue == null)return "";
        String retValue = nValue.getNodeValue();
        return retValue;
    }
        
    private String paperPath;    
}
